package com.zuo.mall.service.impl;

import cn.hutool.core.util.PageUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zuo.mall.common.api.CommonPage;
import com.zuo.mall.dto.PmsProductAttributeCategoryItem;
import com.zuo.mall.entity.PmsProductAttribute;
import com.zuo.mall.entity.PmsProductAttributeCategory;
import com.zuo.mall.entity.PmsProductCategory;
import com.zuo.mall.mapper.PmsProductAttributeCategoryMapper;
import com.zuo.mall.service.PmsProductAttributeCategoryService;
import com.zuo.mall.service.PmsProductAttributeService;
import com.zuo.mall.service.PmsProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class PmsProductAttributeCategoryServiceImpl
        extends ServiceImpl<PmsProductAttributeCategoryMapper, PmsProductAttributeCategory>
        implements PmsProductAttributeCategoryService {

    @Autowired
    private PmsProductAttributeService productAttributeService;


    @Override
    public List<PmsProductAttributeCategoryItem> getListWithAttr() {
        return baseMapper.getListWithAttr();

    }

    @Override
    public CommonPage<PmsProductAttributeCategory> getList(Integer pageSize, Integer pageNum) {
        LambdaQueryWrapper<PmsProductAttributeCategory> queryWrapper = new LambdaQueryWrapper<>();
        int start = PageUtil.getStart(pageNum - 1, pageSize);
        Integer total = baseMapper.selectCount(queryWrapper);
        queryWrapper.last("LIMIT " + start + "," + pageSize);
        List<PmsProductAttributeCategory> pmsProductAttributeCategories = baseMapper.selectList(queryWrapper);
        CommonPage<PmsProductAttributeCategory> commonPage = new CommonPage<>();
        commonPage.setList(pmsProductAttributeCategories);
        commonPage.setTotal(Long.valueOf(total));
        commonPage.setTotalPage(PageUtil.totalPage(total, pageSize));
        return commonPage;
    }

    /**
     * 向数据库添加商品属性分类
     */
    @Override
    public int createNew(String name) {
        PmsProductAttributeCategory pmsProductAttributeCategory = new PmsProductAttributeCategory();
        pmsProductAttributeCategory.setName(name);
        return baseMapper.insert(pmsProductAttributeCategory);
    }

    @Override
    public int update(Long id, String name) {
        // 更新信息
        PmsProductAttributeCategory productAttributeCategory = new PmsProductAttributeCategory();
        productAttributeCategory.setId(id);
        productAttributeCategory.setName(name);
        // 更新条件
        LambdaUpdateWrapper<PmsProductAttributeCategory> pmsProductCategoryLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        pmsProductCategoryLambdaUpdateWrapper.set(PmsProductAttributeCategory::getName, name);
        pmsProductCategoryLambdaUpdateWrapper.eq(PmsProductAttributeCategory::getId, id);
        int update = baseMapper.update(productAttributeCategory, pmsProductCategoryLambdaUpdateWrapper);
        return update;
    }

    @Transactional
    @Override
    public int delete(Long id) {
        // 删除attribute
        int count = productAttributeService.deleteListByProductAttributeCategoryId(id);
        int i = baseMapper.deleteById(id);
        return i;
    }

}
